﻿using System.IO;
using System.Xml.Serialization;
namespace CoreModule
{
    /// <summary>
    /// 串口配置
    /// </summary>
    public class LocalConfigurationInfo
    {
        static string XmlFilePath = "LocalConfigurationInfo.xml";

        /// <summary>
        /// 
        /// </summary>
        public string PortName { get; set; } = string.Empty;

        public string WorkMode { get; set; } = CoreModule.WorkMode.步骤一和步骤二.ToString();

        public static LocalConfigurationInfo Load()
        {
            if (File.Exists(XmlFilePath))
            {
                try
                {
                    using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                    {
                      return (LocalConfigurationInfo)new XmlSerializer(typeof(LocalConfigurationInfo)).Deserialize(fileStream);
                    }
                }
                catch (Exception )
                {
                    File.Delete(XmlFilePath);
                    return new LocalConfigurationInfo();
                }
            }
            return new LocalConfigurationInfo();
            
        }
        /// <summary>
        /// /
        /// </summary>
        public void Save() {
            if (File.Exists(XmlFilePath))
            {
                File.Delete(XmlFilePath);
            }
            try
            {
                using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                {
                    new XmlSerializer(typeof(LocalConfigurationInfo)).Serialize(fileStream, this);
                }
            }
            catch (Exception)
            {
            }
        }
    
       
 
    }
}
